package com.example.zzl.view.fragment.SearchFragment;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzl.R;
import com.example.zzl.adapter.SearchFragment.ListViewBrandAdapter;
import com.example.zzl.databinding.BrandfragmentLayoutBinding;
import com.example.zzl.model.bean.SearchFragment.SearchBrand;
import com.example.zzl.model.cans.Cans;
import com.example.zzl.view.BaseFragment;
import com.example.zzl.view.activity.MainActivity;
import com.example.zzl.view.activity.SearchActivity.AllActivity;
import com.example.zzl.viewmodel.SearchFragment.Classify;

import java.util.ArrayList;

/**
 * Created by zhang on 2017/2/8.
 */

public class BrandFragment extends Fragment implements AdapterView.OnItemClickListener{
    private BrandfragmentLayoutBinding binding;
    private ListView lv;
    private ArrayList<SearchBrand.ResultBeanX> list;
    private ProgressBar progressBar;
//    private ArrayList<SearchBrand.ResultBeanX> results;
//    private ArrayList<SearchBrand.ResultBeanX.ResultBean> resultBeen;
    private ListViewBrandAdapter adapter;
    private Classify classify;
    public static BrandFragment getInstance() {
        Bundle args = new Bundle();
        BrandFragment fragment = new BrandFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        classify = new Classify(Cans.BRAND_URL,"Brand");
        list = new ArrayList<>();
//        list1 = new ArrayList<>();
//        results = new ArrayList<>();
//        resultBeen = new ArrayList<>();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.brandfragment_layout,container,false);
        View v = inflater.inflate(R.layout.brand_listview_head,null);
        progressBar = binding.pbLoad;
        lv = binding.lvBrand;
        lv.setEmptyView(progressBar);
        lv.addHeaderView(v);
        lv.setOnItemClickListener(this);
        //lv.setAdapter(adapter);
        loadData();
        return binding.getRoot();
    }

    private void loadData(){
        MainActivity.handler1 = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what == 0x103){
                    list = MainActivity.resultData.results;
                    adapter = new ListViewBrandAdapter(getActivity(),list);
                    lv.setAdapter(adapter);
                }
            }
        };

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if(i == 0){
            startActivityForResult(new Intent(getActivity(), AllActivity.class),333);
        }else{
            lv.setFocusable(false);
        }
    }
}
